$().ready(function() {
	initDictDatas();
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
//构建字典数据
function initDictDatas(){
	
	var thatDictFields = window.parent.dictFields;
	var thatDictkeys = window.parent.dictKeys;
	var thatDictDatas = window.parent.dicDatas;
	
	$('#platId').bootstrapSelect({
	    url:ctx+'plat/platInfo/listPlatDict',
	    valueField: 'platId',
	    textField: 'platName',
	    placeholder: "请选择所属平台",
	    onChange:function(value){
	    	if(value == null || value == ''){
	    		return false;
	    	}
	    	$('#shopId').bootstrapSelect({
	    		url:ctx+'plat/shopInfo/listShopDict/'+value,
	    		valueField: 'shopId',
	    		textField: 'shopName',
	    		placeholder: "请选择所属门店"
	    	});
	    	$('#shopId').selectpicker('refresh');
	    }
	});
	$('#shopId').bootstrapSelect({
	    valueField: 'shopId',
	    textField: 'shopName',
	    placeholder: "请选择所属门店"
	});
	$('#userFlag').bootstrapSelect({
		data:thatDictDatas['shopUserFlag'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择状态"
	});
	
	
	$('#platId').bootstrapSelect('setValue',platId);
	$('#shopId').bootstrapSelect('setValue',shopId);
	$('#userFlag').bootstrapSelect('setValue',userFlag);
	
	
}
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"wxs/shopUser/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			platId : {
				required : true
			},			
			shopId : {
				required : true
			},			
			userName : {
				required : true,
				minlength:2
			},			
			userPass : {
				required : true,
				minlength:6,
				maxlength:20
			},			
			userPhone : {
				required : true,
				isPhone : true
			},
			userCommunitynum : {
				required : true,
				isIdentity : true
			},
			userFlag : {
				required : true
			}
		},
		messages : {
			platId : {
				required : icon + "请选择所属平台"
			},			
			shopId : {
				required : icon + "请选择所属门店"
			},			
			userName : {
				required : icon + "请输入用户名",
				minlength : icon + "用户名必须两个字符以上",
			},	
			userPass : {
				required : icon + "请输入用户密码",
				minlength : icon + "密码必须6个字符以上",
				maxlength: icon + "密码不能超过20个字符"
			},	
			userPhone : {
				required : icon + "请输入用户手机号"
			},
			userCommunitynum : {
				required : icon + "请输入身份证号"
			},
			userFlag : {
				required : icon + "请选择用户状态"
			}
		},
		errorPlacement: function(error, element) {
			if (element[0].tagName && element[0].tagName.toLowerCase() == 'select'){
				error.appendTo(element.parent().parent());
			}else {					
				error.appendTo(element.parent());
			}
		},
		success:function(lable){
			lable.parents(".tip").remove();
			console.log(lable);
		}
	})
}
function resetPass(){
//	$('#userPass').show();
	$('#userPass').attr('type','text');
	$('#userPass').val('111111');
}